package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
//        Class cls = Class.forName("reflect.Person");
//        Object obj = cls.newInstance();
//
//        Method method = cls.getMethod("sayHello");
//        method.invoke(obj);
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("输入类名：");
            String className = sc.nextLine();
            System.out.println("输入方法名：");
            String methodName = sc.nextLine();
            Class cls = Class.forName(className);
            Object obj = cls.newInstance();

            Method method = cls.getMethod(methodName);
            method.invoke(obj);

        }
    }
}
